var timer = null;
import {
  http
} from "../../utils/index"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    citylist:[],
    list: [],
    toggle: true,
    percent: 0,
    banner: null,
    letter: ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"],
    newcity: ['北京', '上海', '广州', '深圳', '成都', '杭州']
  },

  letterTap(e){
    const Item = e.currentTarget.dataset.item;
      this.setData({
        cityListId: Item
      });
      console.log(this.data.cityListId);
  },

  cityTap(e){
    const val = e.currentTarget.dataset.val || '',
    types = e.currentTarget.dataset.types || '',
    Index = e.currentTarget.dataset.index || '',
    that=this;
  let city = this.data.citySel;
  switch (types) {
    case 'locate':
      //定位内容
      city = this.data.locateCity;
      break;
    case 'national':
      //全国
      city = '全国';
      break;
    case 'new':
      //热门城市
      city = val;
      break;
    case 'list':
      //城市列表
      city = val.cityName;
      break;
  }
  if(city){
    wx.setStorage({
      key: 'city',
      data: city
    })
　　　　//点击后给父组件可以通过bindcitytap事件，获取到cityname的值，这是子组件给父组件传值和触发事件的方法
    this.triggerEvent('citytap', { cityname: city });
  }else{
    console.log('还没有');
    this.getLocate();
  }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getCity()
  },
  getCity() {
    http({
      url: "http://api.yytianqi.com/citylist/id/1",
      data: {},
      method: "GET",
      success: res => {
        let pageData = res.data.list
        this.setData({
          citylist: pageData,
        });
        pageData.forEach((item, i) => {
          item.list.forEach(item => {
            if (item.list) {
              this.data.list.push(item.list[0])
              this.setData({
                list: this.data.list
              })
            } else {
              this.data.list.push(item)
              this.setData({
                list: this.data.list
              })
            }
          })
        });
        var arr = []
        var arra = []
        this.data.letter.forEach(item=>{
            this.data.list.forEach(it =>{
              if(it.en[0].toUpperCase() == item){
                  arra.push(it)
              }
            })
            arr.push({letter:item,data:[...arra]})
        })
        this.setData({
          citylist:arr
        })
        wx.showToast({
          title: '请求成功',
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})